#coding=utf-8
import xadmin
from xadmin import views
from .models import *
from xadmin.layout import Fieldset, Main, Side, Row, FormHelper
from django.utils.translation import ugettext as _
from django.utils import timezone

class NavigationAdmin(object):
    list_display = ['name', 'parent', 'review_count', 'avator', 'url', 'company', 'isShow', 'updated_at', 'created_at']
    search_fields = ['name', ]
    list_filter = ['name', 'parent', 'review_count', 'avator', 'url', 'company', 'isShow', 'updated_at', 'created_at']
    readonly_fields = ['review_count', ]
    model_icon = 'fa fa-arrows'

class HouseTypeAdmin(object):
    list_display = ['name', 'updated_at', 'created_at']
    search_fields = ['name', ]
    list_filter = ['name', 'updated_at', 'created_at']
    model_icon = 'fa fa-cogs'

class HouseBuildingImageInline(object):
    model = HouseBuildingImage
    extra = 0

class HouseBuildingAdmin(object):
    list_display = ['name', 'title', 'region', 'addr',  'show_avator', 'click_count', 'sign_count', 'build_count',  'isEnabled', 'nav', 'updated_at', 'created_at']
    search_fields = ['name', 'title', 'addr', ]
    list_filter = ['name', 'title', 'region', 'addr', 'click_count', 'sign_count', 'build_count',  'isEnabled', 'nav', 'updated_at', 'created_at']
    model_icon = 'fa fa-home'
    readonly_fields = ['click_count', 'sign_count', 'build_count',]
    inlines = [HouseBuildingImageInline]

    def get_form_layout(self):
        self.form_layout = (
            Main(
                Fieldset('',
                            'nav', 
                            css_class='unsort no_title'
                            ),
                Fieldset(_(u"基本信息"),
                            Row('name', 'region'),
                            'addr',
                            'description',
                            'avator',
                            Row('sign_count', 'build_count'),
                            ),
                Fieldset(u"网页信息",
                            'title', 'des', 'keywords', 'click_count', 
                            ), 
            ),
            Side(
                Fieldset(_('Status'),
                            'isEnabled',  
                            ),
            )
        )
        return super(HouseBuildingAdmin, self).get_form_layout()

class HouseStyleAdmin(object):
    list_display = ['name', 'updated_at', 'created_at']
    search_fields = ['name', ]
    list_filter = ['name', 'updated_at', 'created_at']
    model_icon = 'fa fa-random'

class DesignerJobAdmin(object):
    list_display = ['title', 'updated_at', 'created_at']
    search_fields = ['title', ]
    list_filter = ['title', 'updated_at', 'created_at']
    model_icon = 'fa fa-cog'


class DesignerGoodStyleInline(object):
    model = DesignerGoodStyle
    extra = 0

class DesignerAdmin(object):
    list_display = ['name', 'job', 'mobile', 'years', 'get_goodstyles', 'show_avator', 'click_count', 'isEnabled', 'nav', 'updated_at', 'created_at']
    search_fields = ['name', 'mobile', ]
    list_filter = ['name', 'job', 'mobile', 'years', 'click_count', 'isEnabled', 'nav', 'updated_at', 'created_at']
    model_icon = 'fa fa-pencil'
    readonly_fields = ['click_count', ]
    inlines = [DesignerGoodStyleInline]

    def get_form_layout(self):
        self.form_layout = (
            Main(
                Fieldset('',
                            'nav', 
                            css_class='unsort no_title'
                            ),
                Fieldset(_('Personal info'),
                            Row('name', 'job'),
                            Row('years', 'mobile'),
                            'avator',
                            'word',
                            'description',
                            ),
                Fieldset(u"网页信息",
                            'title', 'des', 'keywords', 'click_count', 
                            ), 
            ),
            Side(
                Fieldset(_('Status'),
                            'isEnabled',  
                            ),
            )
        )
        return super(DesignerAdmin, self).get_form_layout()

class CaseImageInline(object):
    model = CaseImage
    extrra = 0

class CaseAdmin(object):
    list_display = ['name', 'title', 'house', 'area', 'housetype', 'housestyle', 'show_avator', 'designer', 'idea_desc', 'click_count', 'isEnabled', 'nav', ]
    search_fields = ['name', 'title', 'idea_desc', ]
    list_filter = ['name', 'title', 'house', 'area', 'housetype', 'housestyle', 'designer', 'idea_desc', 'click_count', 'isEnabled', 'nav', 'updated_at', 'created_at']
    model_icon = 'fa fa-briefcase'
    readonly_fields = ['click_count', ]
    inlines = [CaseImageInline]

    def get_form_layout(self):
        self.form_layout = (
            Main(
                Fieldset('',
                            'nav', 
                            css_class='unsort no_title'
                            ),
                Fieldset(u"基本信息",
                            Row('name', 'house'),
                            Row('housetype', 'housestyle'),
                            Row('area', 'designer'),
                            'avator',
                            'idea_desc',
                            ),
                Fieldset(u"网页信息",
                            'title', 'des', 'keywords', 'click_count', 
                            ), 
            ),
            Side(
                Fieldset(_('Status'),
                            'isEnabled',  
                            ),
            )
        )
        return super(CaseAdmin, self).get_form_layout()


class BannerAdmin(object):
    list_display = ['name', 'title', 'show_avator', 'click_count', 'isEnabled', 'isShow', 'nav', 'updated_at', 'created_at']
    search_fields = ['name', 'title', ]
    list_filter = ['name', 'title', 'click_count', 'isEnabled', 'isShow', 'nav', 'updated_at', 'created_at']
    model_icon = 'fa fa-picture-o'
    readonly_fields = ['click_count', ]
    style_fields = {"rich_desc": "ueditor"}

    def get_form_layout(self):
        self.form_layout = (
            Main(
                Fieldset('',
                            'nav', 
                            css_class='unsort no_title'
                            ),
                Fieldset(u"基本信息",
                            'name',
                            'avator',
                            'rich_desc',
                            ),
                Fieldset(u"网页信息",
                            'title', 'des', 'keywords', 'click_count', 
                            ), 
            ),
            Side(
                Fieldset(_('Status'),
                            'isEnabled',  'isShow',
                            ),
            )
        )
        return super(BannerAdmin, self).get_form_layout()

class ArticleAdmin(object):
    list_display = ['name', 'title', 'show_avator', 'description', 'click_count', 'isEnabled', 'writor', 'issubmit', 'reviewer', 'review_status', 'review_time', 'nav', 'updated_at', 'created_at']
    search_fields = ['name', 'title', 'description']
    list_filter = ['name', 'title', 'description', 'click_count', 'isEnabled', 'writor', 'issubmit', 'reviewer', 'review_status', 'review_time', 'nav', 'updated_at', 'created_at']
    model_icon = 'fa fa-book'
    readonly_fields = ['click_count', 'writor', 'reviewer', 'review_status', 'review_time', 'isEnabled', ]
    style_fields = {"rich_content": "ueditor"}
    ordering = ['-created_at', ]

    def get_form_layout(self):
        self.form_layout = (
            Main(
                Fieldset('',
                            'nav', 
                            css_class='unsort no_title'
                            ),
                Fieldset(u"基本信息",
                            'name',
                            'avator',
                            'description',
                            'rich_content',
                            ),
                Fieldset(u"网页信息",
                            'title', 'des', 'keywords', 'click_count', 
                            ), 
                Fieldset(u"审核信息",
                            Row('writor', 'reviewer'),
                            Row('review_status', 'review_time'),
                            'isEnabled',
                            ), 
            ),
            Side(
                Fieldset(_('Status'),
                            'issubmit', 
                            ),
            )
        )
        return super(ArticleAdmin, self).get_form_layout()

    def queryset(self):
        qs = super(ArticleAdmin, self).queryset()
        qs = qs.filter(issubmit=False)
        return qs

    def save_models(self):
        # 在保存课程的顺便保存作者
        obj = self.new_obj
        request = self.request
        obj.writor = request.user
        obj.save()

class ReviewArticleAdmin(object):
    list_display = ['name', 'title', 'show_avator', 'description', 'review_status', 'click_count', 'isEnabled', 'writor', 'issubmit', 'reviewer', 'review_time', 'nav', 'updated_at', 'created_at']
    search_fields = ['name', 'title', 'description']
    list_filter = ['name', 'title', 'description', 'click_count', 'isEnabled', 'writor', 'issubmit', 'reviewer', 'review_status', 'review_time', 'nav', 'updated_at', 'created_at']
    model_icon = 'fa fa-book'
    readonly_fields = ['name', 'title', 'des', 'keywords', 'avator', 'description', 'click_count', 'isEnabled', 'writor', 'issubmit', 'reviewer', 'review_time', 'nav', 'rich_content', ]
    #style_fields = {"rich_content": "ueditor"}
    ordering = ['-created_at', ]

    def get_form_layout(self):
        self.form_layout = (
            Main(
                Fieldset('',
                            'nav', 
                            css_class='unsort no_title'
                            ),
                Fieldset(u"基本信息",
                            'name',
                            'avator',
                            'description',
                            'rich_content',
                            ),
                Fieldset(u"网页信息",
                            'title', 'des', 'keywords', 'click_count', 
                            ), 
                Fieldset(u"审核信息",
                            Row('writor', 'reviewer'),
                            Row('issubmit', 'review_time'),
                            'isEnabled',
                            ), 
            ),
            Side(
                Fieldset(_('Status'),
                            'review_status', 
                            ),
            )
        )
        return super(ReviewArticleAdmin, self).get_form_layout()

    def queryset(self):
        qs = super(ReviewArticleAdmin, self).queryset()
        qs = qs.filter(issubmit=True).filter(review_status=0)
        return qs

    def save_models(self):
        # 在保存课程的顺便保存审核信息
        obj = self.new_obj
        request = self.request
        obj.reviewer = request.user
        obj.review_time = timezone.now()
        obj.save()


class ReviewedArticleAdmin(object):
    list_display = ['name', 'title', 'show_avator', 'description', 'review_status', 'click_count', 'isEnabled', 'writor', 'issubmit', 'reviewer', 'review_time', 'nav', 'updated_at', 'created_at']
    search_fields = ['name', 'title', 'description']
    list_filter = ['name', 'title', 'description', 'click_count', 'isEnabled', 'writor', 'issubmit', 'reviewer', 'review_status', 'review_time', 'nav', 'updated_at', 'created_at']
    model_icon = 'fa fa-book'
    readonly_fields = ['name', 'title', 'des', 'keywords', 'avator', 'description', 'click_count', 'isEnabled', 'writor', 'issubmit', 'reviewer', 'review_time', 'nav', 'rich_content', 'review_status', ]
    #style_fields = {"rich_content": "ueditor"}
    ordering = ['-created_at', ]

    def get_form_layout(self):
        self.form_layout = (
            Main(
                Fieldset('',
                            'nav', 
                            css_class='unsort no_title'
                            ),
                Fieldset(u"基本信息",
                            'name',
                            'avator',
                            'description',
                            'rich_content',
                            ),
                Fieldset(u"网页信息",
                            'title', 'des', 'keywords', 'click_count', 
                            ), 
                Fieldset(u"审核信息",
                            Row('writor', 'reviewer'),
                            Row('issubmit', 'review_time'),
                            'isEnabled',
                            ), 
            ),
            Side(
                Fieldset(_('Status'),
                            'review_status', 
                            ),
            )
        )
        return super(ReviewedArticleAdmin, self).get_form_layout()

    def queryset(self):
        qs = super(ReviewedArticleAdmin, self).queryset()
        qs = qs.exclude(review_status=0)
        return qs

class GlobalActivityAdmin(object):
    list_display = ['name', 'title', 'show_avator', 'description', 'click_count', 'isEnabled', 'nav', 'updated_at', 'created_at']
    search_fields = ['name', 'title', 'description', ]
    list_filter = ['name', 'title', 'click_count', 'isEnabled', 'nav', 'updated_at', 'created_at']
    model_icon = 'fa fa-bolt'
    readonly_fields = ['click_count', ]
    style_fields = {"rich_content": "ueditor"}
    ordering = ['-created_at', ]

    def get_form_layout(self):
        self.form_layout = (
            Main(
                Fieldset('',
                            'nav', 
                            css_class='unsort no_title'
                            ),
                Fieldset(u"基本信息",
                            'name',
                            'avator',
                            'description',
                            'rich_content',
                            ),
                Fieldset(u"网页信息",
                            'title', 'des', 'keywords', 'click_count', 
                            ), 
            ),
            Side(
                Fieldset(_('Status'),
                            'isEnabled',
                            ),
            )
        )
        return super(GlobalActivityAdmin, self).get_form_layout()


class FurnitureTypeAdmin(object):
    list_display = ['name', 'updated_at', 'created_at']
    search_fields = ['name', ]
    list_filter = ['name', 'updated_at', 'created_at']
    model_icon = 'fa fa-plus-square-o'

class FurnitureBrandAdmin(object):
    list_display = ['name', 'updated_at', 'created_at']
    search_fields = ['name', ]
    list_filter = ['name', 'updated_at', 'created_at']
    model_icon = 'fa fa-flag'


class FurnitureImageInline(object):
    model = FurnitureImage
    extra = 0

class FurnitureAdmin(object):
    list_display = ['name', 'show_avator', 'price', 'ftype', 'brand', 'series', 'model', 'spec', 'style', 'caizhi',   'click_count', 'isEnabled', 'nav']
    search_fields = ['name', 'title', ]
    list_filter = ['name', 'price', 'ftype', 'brand', 'series', 'model', 'spec', 'style', 'caizhi', 'click_count', 'isEnabled', 'nav', 'updated_at', 'created_at']
    model_icon = 'fa fa-desktop'
    readonly_fields = ['click_count', ]
    inlines = [FurnitureImageInline]

    def get_form_layout(self):
        self.form_layout = (
            Main(
                Fieldset('',
                            'nav', 
                            css_class='unsort no_title'
                            ),
                Fieldset(u"基本信息",
                            'name',
                            'avator',
                            Row('price', 'ftype'),
                            Row('brand', 'series'),
                            Row('model', 'spec'),
                            Row('style', 'caizhi'),
                            ),
                Fieldset(u"网页信息",
                            'title', 'des', 'keywords', 'click_count', 
                            ), 
            ),
            Side(
                Fieldset(_('Status'),
                            'isEnabled',
                            ),
            )
        )
        return super(FurnitureAdmin, self).get_form_layout()




xadmin.site.register(Navigation, NavigationAdmin)
xadmin.site.register(HouseType, HouseTypeAdmin)
xadmin.site.register(HouseBuilding, HouseBuildingAdmin)
xadmin.site.register(HouseStyle, HouseStyleAdmin)
xadmin.site.register(DesignerJob, DesignerJobAdmin)
xadmin.site.register(Designer, DesignerAdmin)
xadmin.site.register(Case, CaseAdmin)
xadmin.site.register(Banner, BannerAdmin)
xadmin.site.register(Article, ArticleAdmin)
xadmin.site.register(ReviewArticle, ReviewArticleAdmin)
xadmin.site.register(ReviewedArticle, ReviewedArticleAdmin)
xadmin.site.register(GlobalActivity, GlobalActivityAdmin)
xadmin.site.register(FurnitureType, FurnitureTypeAdmin)
xadmin.site.register(FurnitureBrand, FurnitureBrandAdmin)
xadmin.site.register(Furniture, FurnitureAdmin)

